package br.com.neb.caronamanager.view;

import java.util.Date;
import java.util.List;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import br.com.neb.caronamanager.R;
import br.com.neb.caronamanager.adapter.CaronaAdapter;
import br.com.neb.caronamanager.model.Carona;
import br.com.neb.caronamanager.model.dao.DB;

public class CaronaFragment extends Fragment{
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View caronaView = inflater.inflate(R.layout.fragment_carona, container,false);
		final DB db = new DB(caronaView.getContext());
		
		List<Carona> itemList = db.search();
		
		final CaronaAdapter adapter = new CaronaAdapter(itemList, caronaView.getContext());
		
		final ListView listview = (ListView)caronaView.findViewById(R.id.list_caronas);
		listview.setAdapter(adapter);
		
		Button btn = (Button)caronaView.findViewById(R.id.btnShowDate);
		btn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Integer date = (int)new Date().getTime()/1000;
				db.insert(new Carona(date,"4545",1));
				adapter.getData().clear();
				adapter.getData().addAll(db.search());
				adapter.notifyDataSetChanged();
			}
		});
		
		
		
		return caronaView;
	}

}
